import 'package:flutter/material.dart';

class FormSecondPage extends StatefulWidget {
  FormSecondPage({Key key}) : super(key: key);

  @override
  _FormSecondPageState createState() => _FormSecondPageState();
}

class _FormSecondPageState extends State<FormSecondPage> {

  String username;
  int sex=1;
  String info='';
  List hobby =[
    {
      'checked': true,
      'title': '吃饭'
    },
    {
      'checked': true,
      'title': '睡觉'
    },
    {
      'checked': true,
      'title': '写代码'
    },
  ];

  List<Widget> _getHobby(){
    List<Widget> tempList = [];
    for(var i=0; i < this.hobby.length; i++){
      // 横向
      // tempList.add(Text(this.hobby[i]['title']+':'));
      // tempList.add(
      //   Checkbox(
      //     value: this.hobby[i]["checked"],
      //     onChanged: (value){
      //       setState(() {
      //         this.hobby[i]['checked']=value;            
      //       });
      //     },
      //   )
      // ); 

      // 纵向
      tempList.add(
        Row(
          children: <Widget>[
            Text(this.hobby[i]['title']+':'),
            Checkbox(
              value: this.hobby[i]["checked"],
              onChanged: (value){
                setState(() {
                  this.hobby[i]['checked']=value;            
                });
              },
            )
          ],
        )
      );
    }
    return tempList;
  }

  void _sexChanged(val){
      setState(() {
        this.sex=val;   
      });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('学员信息登录系统'),
      ),
      body: Padding(
        padding: EdgeInsets.all(20),
        child: Column(
          children: <Widget>[
                // Text('data'),
                TextField(
                  decoration: InputDecoration(
                    hintText: '请输入用户名',
                  ),
                  // controller: username,
                  onChanged: (value){
                    setState(() {
                      this.username = value;
                    });
                  },
                ),
                SizedBox(height: 20),
                Row(
                  children: <Widget>[
                    Text('男'),
                    Radio(
                      value: 1,
                      onChanged: this._sexChanged,
                      groupValue: this.sex,
                    ),
                    SizedBox(width: 20),
                    Text('女'),
                    Radio(
                      value: 2,
                      onChanged: this._sexChanged,
                      groupValue: this.sex,
                    ),
                  ],
                ),
                // 爱好
                SizedBox(height: 20),
                Wrap(
                  children: this._getHobby(),
                ),
                SizedBox(height: 20),
                TextField(
                  maxLines: 4,
                  decoration: InputDecoration(
                    hintText: '描述',
                    border: OutlineInputBorder()
                  ),
                  onChanged: (value){
                    setState(() {
                      this.info = value;
                    });
                  },
                ),
                SizedBox(height: 20),
                 Container(
                    width: double.infinity,// 宽度自适应
                    child: RaisedButton(
                      child: Text('提交信息'),
                      onPressed: (){
                        print(this.sex);
                        print(this.username);
                        print(this.hobby);
                      },
                      color: Colors.blue,
                      textColor: Colors.white,
                 ),
                )

          ],
        ),
      ),
    );
  }
}